package com.example.dormitorysystem.controller;


import com.example.dormitorysystem.common.aop.OperationLogAnnotation;
import com.example.dormitorysystem.entity.Buildings;
import com.example.dormitorysystem.entity.pojo.BuildingOutput;
import com.example.dormitorysystem.service.BuildingsService;
import com.example.dormitorysystem.utils.Response;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 吉吉王国组
 * @since 2022-11-26
 */
@RestController
@RequestMapping("")
public class BuildingsController {
    @Resource
    private BuildingsService buildingsService;


    @GetMapping("/room/building")
    @OperationLogAnnotation(operModul = "寝室资源模块", operType = "BuildingInfo", operDesc = "查阅寝室楼的具体信息")
    public Response getBuildingInfo(@RequestParam("id") Integer id,
                                    HttpServletRequest request, HttpServletResponse response) {
        Buildings buildings = buildingsService.getBuildingInfoById(id, request);
        if (buildings == null) return Response.error().message("此楼不参与寝室分配");
        Map<String, Object> res = new HashMap<>();
        res.put("name", buildings.getName());
        res.put("describe", buildings.getDescribes());
        res.put("image_url", buildings.getImageUrl());
        return Response.success().data(res);
    }


    @GetMapping("/room/buildinglist")
    @OperationLogAnnotation(operModul = "寝室资源模块", operType = "BuildingList", operDesc = "查阅寝室楼列表")
    public Response getBuildingList(HttpServletRequest request) {
        //返回全部寝室的列表，从列表中选择信息加入到 temp 的HashMap中，再将temp加入到arry中，将arry加入到
        //现在有了这个list
        //实际上是不需要将request传入的
        List<Buildings> buildingsList = buildingsService.getBuildingList();
        // TODO: 2022/11/28 这里添加一个设置，对于不参与选择的楼不列入
        //将data放入到这个res中
        HashMap<String, Object> res = new HashMap<>();
        ArrayList<Object> rows = new ArrayList<>();
        for (Buildings buildings : buildingsList) {
            //做一个rows类，就是array中的object对象
            BuildingOutput buildingOutput = new BuildingOutput(buildings.getId(), buildings.getName());
            rows.add(buildingOutput);
        }
        res.put("rows", rows);
        return Response.success().data(res);
    }

}

